package com.ithema.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ithema.reggie.common.Page;
import com.ithema.reggie.common.R;
import com.ithema.reggie.entity.Category;
import com.ithema.reggie.entity.Dish;
import com.ithema.reggie.dto.DishDto;
import com.ithema.reggie.entity.DishFlavor;
import com.ithema.reggie.mapper.CategoryMapper;
import com.ithema.reggie.mapper.DishFlavorMapper;
import com.ithema.reggie.mapper.DishMapper;
import com.ithema.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public R save(DishDto dishDto) {

        //1.保存菜品
        //1.1给菜品设置排序，两时间
        dishDto.setSort(0);//因为页面上没有,所以统一值

        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(dishDto.getCreateTime());

        //1.2 调用dishMapper完成保存
        //注意:返回新增记录的主键
        dishMapper.save(dishDto);

        //2.保存菜品的口味
        //2.1 获取口味集合,遍历,获取每个口味对象
        List<DishFlavor> flavors = dishDto.getFlavors();
        if (CollUtil.isNotEmpty(flavors)) {
            flavors.forEach(f->{
                //2.2 给口味对象设置 菜品id,俩时间,俩用户id
                f.setDishId(dishDto.getId());

                f.setCreateTime(dishDto.getCreateTime());
                f.setUpdateTime(dishDto.getUpdateTime());

                f.setCreateUser(dishDto.getCreateUser());
                f.setUpdateUser(dishDto.getUpdateUser());
            });

            //2.3保存所有的口味对象
            dishFlavorMapper.bathchSave(flavors);
        }

        return R.success(null);
    }

    //菜品分页
    @Override
    public R findByPage(Integer pageNum, Integer pageSize, String name) {

        //1.查询菜品分类类表，吧list转成map（分类id，分类名称）
        List<Category> categoryList = categoryMapper.findAllByType(1);
        Map<Long,String> categoryMap = new HashMap<>();

        categoryList.forEach(c -> {
            categoryMap.put(c.getId(),c.getName());
        });

        //2.设置分页参数
        PageHelper.startPage(pageNum,pageSize);

        //3.根据名称查询所有
        List<Dish> dishList = dishMapper.findByPage(name);

        //4.将查询的集合封装成PageInfo
        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);

        //5.新建一个DtoList,遍历查询的集合,将dish封装成dishDto ,且给dto设置分类名称,将dto放入dtoList中
        List<DishDto> dtoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(dishList)) {
            dishList.forEach(dish -> {
                //将dish封装成dishDto ,
                DishDto dto = new DishDto();

                //好多工具类都提供bean属性拷贝的操作,只要属性名能对应上就会复制过去,我使用spring中
                BeanUtils.copyProperties(dish,dto);

                //且给dto设置分类名称,
                dto.setCategoryName(categoryMap.get(dish.getCategoryId()));

                //将dto放入dtoList中
                dtoList.add(dto);
            });
        }
        //6.返回page
        return R.success(new Page<>(dtoList,pageInfo.getTotal()));
    }

    /**
     * 作用：
     * @param id
     * @return
     */
    @Override
    public R findById(Long id) {

        //1.查询菜品基本信息
        Dish dish = dishMapper.findById(id);

        //2.查询菜品口味信息
        List<DishFlavor> flavors = dishFlavorMapper.findByDishId(id);

        //3.将两个信息封装dto对象返回
        DishDto dto = new DishDto();
        BeanUtils.copyProperties(dish,dto);
        dto.setFlavors(flavors);

        return R.success(dto);
    }

    //更新菜品带上口味
    @Override
    public R update(DishDto dishDto) {

        //1.给dish修改时间,更新dish
        dishDto.setUpdateTime(LocalDateTime.now());
        dishMapper.update(dishDto);

        //2.根据dishId先删除口味表中所有数据
        dishFlavorMapper.deleteByDishId(dishDto.getId());

        //3.获取页面传递过来的口味对象,批量保存
        List<DishFlavor> flavors = dishDto.getFlavors();
        if (CollUtil.isNotEmpty(flavors)) {
            flavors.forEach(f -> {
                //2.2 给口味对象设置 菜品id,俩时间,俩用户id
                f.setDishId(dishDto.getId());

                f.setUpdateTime(dishDto.getUpdateTime());
                f.setCreateTime(dishDto.getCreateTime());

                f.setCreateUser(dishDto.getCreateUser());
                f.setUpdateUser(dishDto.getUpdateUser());
            });
            //2.3 保存所有的口味对象
            dishFlavorMapper.bathchSave(flavors);
        }


        return R.success(null);
    }

    /**
     * 方法作用： 根据菜品类别的id查找的在售菜品
     * @param categoryId
     * @return
     */
    @Override
    public R findByCategoryId(Long categoryId) {
        List<Dish> dishlist = dishMapper.findOnSaleByCategoryId(categoryId);
        //遍历dishList,获取每个菜品dish,查询每个菜品的口味列表,将菜品和口味列表封装DishDto对象,将封装好的对象放入dtoList

        //1.创建DtoLIst
        List<DishDto> dtoList = new ArrayList<>();

        //2.遍历dishList,获取每个菜品dish
        if (CollUtil.isNotEmpty(dishlist)) {
            for (Dish dish : dishlist) {

                //3.查询每个菜品的口味列表
                List<DishFlavor> flavors = dishFlavorMapper.findByDishId(dish.getId());

                //4.将菜品和口味列表封装DishDto对象
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(dish,dishDto);
                dishDto.setFlavors(flavors);

                //5.将封装好的对象放入dtoList中
                dtoList.add(dishDto);
            }

        }
        return R.success(dtoList);
    }

    /**
     * 方法作用： 根据菜品类别的id查找的在售菜品
     * @param categoryId
     * @return
     */
    /*
    @Override
    public R findByCategoryId(Long categoryId) {
        List<Dish> list = dishMapper.findOnSaleByCategoryId(categoryId);
        return R.success(list);
    }*/



}
